package com.ys.servers.points.service.impl;

import com.ys.servers.common.data.ReturnData;
import com.ys.servers.common.data.UserType;
import com.ys.servers.common.util.CommonUtils;
import com.ys.servers.common.util.TimeUtils;
import com.ys.servers.points.dao.UserSignInfoDao;
import com.ys.servers.points.dao.impl.UserSignInfoDaoImpl;
import com.ys.servers.points.data.UserSignInfo;
import com.ys.servers.points.service.UserSignInfoService;
import io.vertx.core.Future;

import java.util.Map;
import java.util.HashMap;

import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;

/**
 * Created by taotao on 2017/5/24.
 */
public class UserSignInfoServiceImpl implements UserSignInfoService {

    private UserSignInfoDao userSignInfoDao;

    public UserSignInfoDao getUserSignInfoDao() {
        return userSignInfoDao;
    }

    public UserSignInfoServiceImpl setUserSignInfoDao(UserSignInfoDao userSignInfoDao) {
        this.userSignInfoDao = userSignInfoDao;
        return this;
    }

    public UserSignInfoServiceImpl() {
        setUserSignInfoDao(new UserSignInfoDaoImpl());
    }

    @Override
    public UserSignInfoService userSign(String userId, UserType userType, Handler<AsyncResult<UserSignInfoData>> resultHandler) {

        Future<UserSignInfoData> future = Future.future();
        future.setHandler(resultHandler);


        getUserSignInfo(userId, userType, getUserSignInfoRes -> {
            UserSignInfoData userSignInfoData = new UserSignInfoData();
            //UserSignInfo userSignInfo = null;
            if (CommonUtils.validAsyncResult(getUserSignInfoRes)) {
                userSignInfoData = getUserSignInfoRes.result();
                if (TimeUtils.today(userSignInfoData.getLastSignInTime())) {
                    future.complete(null);
                    return;
                }
               /* if(userSignInfo.getFirstSignInTime()==0)
                    userSignInfo.setFirstSignInTime(System.currentTimeMillis());*/
                if (!TimeUtils.continuousDay(userSignInfoData.getLastSignInTime())) {//签到断了
                    userSignInfoData.setContinuousDay(1).setLastSignInTime(System.currentTimeMillis());
                } else {//正常签到
                    userSignInfoData.setLastSignInTime(System.currentTimeMillis()).setContinuousDay(userSignInfoData.getContinuousDay() + 1);
                }
                getUserSignInfoDao().userSign(new UserSignInfo().setUserType(userType).setUserId(userId).setLastSignInTime(userSignInfoData.getLastSignInTime())
                        .setContinuousDay(userSignInfoData.getContinuousDay())).setHandler(signRes -> {
                    if (CommonUtils.validAsyncResult(signRes)) {
                        future.complete(genDataFromInfo(signRes.result()));
                    } else {
                        future.complete(null);
                    }
                });
            }
        });
        return this;
    }

    @Override
    public UserSignInfoService getUserSignInfo(String userId, UserType userType, Handler<AsyncResult<UserSignInfoData>> resultHandler) {

        getUserSignInfoDao().getUserSignInfo(userId, userType).setHandler(res -> {
            Future<UserSignInfoData> data = Future.future();
            if (CommonUtils.validAsyncResult(res)) {
                data.setHandler(resultHandler).complete(genDataFromInfo(res.result()));
            } else {
                long now = System.currentTimeMillis();
                UserSignInfo userSignInfo = new UserSignInfo()//.setFirstSignInTime(0)
                        .setLastSignInTime(0).setUserId(userId).setContinuousDay(0).setUserType(userType);
                getUserSignInfoDao().userSign(userSignInfo);
                data.setHandler(resultHandler).complete(genDataFromInfo(userSignInfo));
            }
        });

        return this;
    }

    private UserSignInfoData genDataFromInfo(UserSignInfo userSignInfo) {
        return new UserSignInfoData().setLastSignInTime(userSignInfo.getLastSignInTime())
                .setContinuousDay(userSignInfo.getContinuousDay())
                .setTodaySign(TimeUtils.today(userSignInfo.getLastSignInTime()));
    }
}
